﻿using ACompany.OrderProcessing.Infrastructure.CustomExceptions;
using ACompany.OrderProcessing.Model;
using StructureMap;

public class OrderPresenter
{
    private IOrderDetailView _view;
    private IOrderService _OrderService;

    public OrderPresenter(IOrderDetailView view)
        : this(view, ObjectFactory.GetInstance<IOrderService>())
    {
    }

    public OrderPresenter(IOrderDetailView view, IOrderService orderService)
    {
        _view = view;
        _OrderService = orderService;
    }


    public void Display()
    {
        clearInfoAndErrorMessages();

        Order order = _OrderService.FindBy(_view.OrderId);

        if (order != null)
        {
            UpdateViewFrom(order);
        }
        else
        {
            _view.ErrorMessage(string.Format("Order Id {0} does not exist", _view.OrderId));
        }

    }

    private void UpdateViewFrom(Order order)
    {
        _view.Order = order;
        _view.CancelOrderButtonVisible = order.CanCancel();
        _view.ShipOrderButtonVisible = order.CanProcess();


        if (order.Status == OrderStatus.Placed & order.HaveEnoughStockToProcess() == false)
        {
            _view.ErrorMessage("We don't have enough stock to fulfill this order.");

        }

    }

    public void Cancel()
    {
        Cancel(_OrderService.FindBy(_view.OrderId));
    }


    public void Cancel(Order order)
    {
        clearInfoAndErrorMessages();

        if (order != null)
        {
            if (order.CanCancel())
            {
                order.Cancel();
                _OrderService.Save(order);
                UpdateViewFrom(order);
                _view.InfoMessage("This order has been cancelled.");
            }
            else
            {
                _view.ErrorMessage("This order cannot be cancelled.");
            }
        }
        else
        {
            _view.ErrorMessage(string.Format("Order Id {0} does not exist", _view.OrderId));
        }

    }

    public void Process()
    {
        Process(_OrderService.FindBy(_view.OrderId));
    }


    public void Process(Order order)
    {
        clearInfoAndErrorMessages();

        if (order != null)
        {
            if (order.CanProcess())
            {
                try
                {
                    _OrderService.Process(order);
                    UpdateViewFrom(order);
                    _view.InfoMessage("This order has been processed.");
                }
                catch (ConcurrencyException ex)
                {
                    _view.ErrorMessage(ex.Message);
                }

            }
            else
            {
                _view.ErrorMessage("This order cannot be processed.");
            }
        }
        else
        {
            _view.ErrorMessage(string.Format("Order Id {0} does not exist", _view.OrderId));
        }

    }

    private void clearInfoAndErrorMessages()
    {
        _view.ErrorMessage(string.Empty);
        _view.InfoMessage(string.Empty);
    }

}